package cn.bluemobi.dylan.step.ui.my.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpFragment;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.StepData;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.presenter.contract.my.MyContract;
import cn.bluemobi.dylan.step.presenter.my.fragment.MyPresenter;
import cn.bluemobi.dylan.step.ui.login.LoginActivity;
import cn.bluemobi.dylan.step.ui.my.activity.MyInfoActivity;
import cn.bluemobi.dylan.step.ui.my.activity.MyOrderActivity;
import cn.bluemobi.dylan.step.ui.my.activity.ScoreActivity;
import cn.bluemobi.dylan.step.ui.my.activity.StepcountActivity;
import cn.bluemobi.dylan.step.utils.common.LogUtils;

/**
 * Created by $Liudan on 2018/11/21 0021.
 */
public class MyFragment extends BaseMvpFragment<MyPresenter> implements MyContract.View {

    @BindView(R.id.ly_my)
    LinearLayout ly_my;
    @BindView(R.id.ly_stepcount)
    LinearLayout ly_stepcount;
    @BindView(R.id.ly_integral)
    LinearLayout ly_integral;
    @BindView(R.id.ly_myOrder)
    LinearLayout ly_myOrder;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;

    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_stepcount)
    TextView tv_stepcount;
    @BindView(R.id.tv_Score)
    TextView tv_Score;
    @BindView(R.id.tv_myOder)
    TextView tv_myOder;

    private User user;
    private MemberAccount memberAccount; //积分
    private int todtyStepCount;

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.my;
    }

    @Override
    protected void initEventAndData() {
        initRefreshLoadMore();
        mPresenter.preUser();
        mPresenter.dbTodayStepcount();

    }

    private void initRefreshLoadMore() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.dbTodayStepcount();
                if (user != null) {
                    mPresenter.getMemberAccount(user.memberId);
                    mPresenter.httpGoodsOrderList(user.memberId);
                } else {
                    mPresenter.preUser();
                }
            }
        });
        refreshLayout.setEnableLoadMore(false);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LogUtils.debug(this.getClass().getName(), "1111111111111111111hidden==" + hidden);
        if (!hidden) {
            mPresenter.dbTodayStepcount();
        }
    }


    private void initInfo() {
//        Glide.with(this).load(user.memberPhone).into(img_head);
        if (user != null) {
            img_head.setImageResource(R.mipmap.my_message);
            tv_nickname.setText(user.memberNickName);

            mPresenter.getMemberAccount(user.memberId);
            mPresenter.httpGoodsOrderList(user.memberId);
        }

    }

    @OnClick({R.id.ly_my, R.id.ly_stepcount, R.id.ly_integral, R.id.ly_myOrder})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.ly_my:
                intent = new Intent(getActivity(), MyInfoActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.ly_stepcount:
                intent = new Intent(getActivity(), StepcountActivity.class);
                intent.putExtra("todtyStepCount", todtyStepCount);
                startActivity(intent);
                break;
            case R.id.ly_integral:
                intent = new Intent(getActivity(), ScoreActivity.class);
                intent.putExtra("MemberAccount", memberAccount);
                startActivity(intent);
                break;
            case R.id.ly_myOrder:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("MemberAccount", memberAccount);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showUserInfo(User user) {
        this.user = user;
        initInfo();
    }

    @Override
    public void showStepcount(StepData stepData) {
        todtyStepCount = stepData.getStepNumber();
        tv_stepcount.setText(stepData.getStepNumber() + "步");
    }

    @Override
    public void showAccountScore(MemberAccount memberAccount) {
        if (getActivity().isFinishing()) return;
        this.memberAccount = memberAccount;
        tv_Score.setText(memberAccount.accountScore + "分");
        refreshLayout.finishRefresh();
    }

    @Override
    public void httpOrderSuceess(int orderTotalSize) {
        tv_myOder.setText(orderTotalSize+"");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) { //退出账户
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        }
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void stateError() {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void closeDialog() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }


}
